<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page">
      <template #headerSearch-append="scope">
        <el-select v-model="scope.searchForm.stockNo" clearable placeholder="请选择仓库" style="margin-right: 10px" size="small">
          <el-option v-for="item in dataList" :key="item.fNumber" :label="item.fName" :value="item.fNumber"></el-option>
        </el-select>
        <div>
          <span style="font-size: 12px; padding-right: 4px; color: #666666">物料</span>
          <el-select filterable remote :remote-method="e => getMaterialList(e)" v-model="scope.searchForm.materialNo" size="small" clearable class="material" placeholder="请输入物料编码或者名称" style="margin-right: 10px">
            <el-option v-for="item in materialList" :key="item.fNumber" :label="item.fNumber + '：' + item.fName + ' ' + item.fSpecification" :value="item.fNumber" />
          </el-select>
        </div>
        <div>
          <span style="font-size: 12px; padding-right: 4px; color: #666666">物料批量</span>
          <el-select filterable remote :remote-method="e => getMaterialList(e)" v-model="scope.searchForm.batchMaterialNo" size="small" clearable class="material" placeholder="请输入物料编码或者名称" style="margin-right: 10px">
            <el-option v-for="item in materialList" :key="item.fNumber" :label="item.fNumber + '：' + item.fName + ' ' + item.fSpecification" :value="item.fNumber" />
          </el-select>
        </div>
      </template>
      <template #headerSearch-btn-append="{ searchForm }">
        <el-button type="warning" size="small" icon="el-icon-download" @click="doDownload(searchForm)">导出即时库存</el-button>
        <el-button type="warning" size="small" icon="el-icon-download" @click="doDownloadTray(searchForm)">导出即时库存托盘</el-button>
        <el-button type="warning" size="small" icon="el-icon-download" @click="doDownloadTotal(searchForm)">导出即时库存总重</el-button>
        <el-button type="warning" size="small" icon="el-icon-download" @click="doSyncInventory">传入唯一码和箱码</el-button>
      </template>
      <template #table-handle-edit="scope">
        <el-button type="text" icon="el-icon-view" @click="gotoDetail(scope.row.id)">查看</el-button>
      </template>
      <template #table-boxCount="scope">
        {{ scope.row.boxCount || '-' }}
      </template>
      <template #table-count="scope">
        {{ scope.row.count? `${Number(scope.row.count.toFixed(4))}${scope.row.unit}`: '-' }}
      </template>
      <template #table-stockNo="scope">
        {{ getStockName(scope.row.stockNo) }}
      </template>
      <template #footerBtn="data">
        箱数合计:{{ data.searchResult.totalBoxCount }}, 重量合计:{{ data.searchResult.totalCount? Number(data.searchResult.totalCount.toFixed(6)): data.searchResult.totalCount }}
      </template>
<!--      <template #table-handle-prepend="scope">-->
<!--        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.billNo, scope.row.stockNo)">查看</el-button>-->
<!--      </template>-->
    </vb-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      reloadMaterial: false,
      materialList: [],
      stockList: [],
      dataList: [],
    }
  },
  async mounted() {
    await this.getStock()
    await this.selectStock()
  },
  methods: {
    async doSyncInventory(){
      this.loading = true
      try {
        let res = await this.callApi('sync_inventory_serial')
        if (!res.res) return
        // this.dataList = res.data
      } finally {
        this.loading = false
      }
    },
    async selectStock() {
      this.loading = true
      try {
        let res = await this.callApi('get_stock')
        if (!res.res) return
        this.dataList = res.data
      } finally {
        this.loading = false
      }
    },
    async doDownload(searchForm) {
        this.loading = true
        try {
            let res = await this.callApi('download_inventory', searchForm)
            if (!res.res) return
        } finally {
            this.loading = false
        }
    },
    async doDownloadTray(searchForm) {
        this.loading = true
        try {
            let res = await this.callApi('download_inventory_tray', searchForm)
            if (!res.res) return
        } finally {
            this.loading = false
        }
    },
    async doDownloadTotal(searchForm) {
        this.loading = true
        try {
            let res = await this.callApi('download_inventory_total', searchForm)
            if (!res.res) return
        } finally {
            this.loading = false
        }
    },
      async doDownloadDetail(searchForm) {
      this.loading = true
      try {
        let res = await this.callApi('download_inventory_detail', searchForm)
        if (!res.res) return
      } finally {
        this.loading = false
      }
    },
    async getMaterialList(val) {
      if (!val) {
        this.materialList = []
        return
      }
      this.materialLoading = true
      try {
        const res = await this.callApi('get_material_by_key', { key: val, pageSize: 20 })
        if (!res.res) return
        this.materialList = res.data
      } finally {
        this.materialLoading = false
      }
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name + ':' + this.stockList[i].key;
          }
        }
      }
      return '';
    },
    gotoDetail(id = '') {
      let url = '/inventory/inventoryDetail'
      if (id) {
        url += `?id=${ id }`
      }
      this.$router.push(url)
    }
  }
}
</script>

<style scoped>
</style>
